package com.koudai.reader.conponment;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.koudai.reader.ReadBookActivity;
import com.koudai.reader.android.R;
import com.koudai.reader.util.Constant;
import com.koudai.reader.vo.EndUser;
import com.koudai.reader.vo.ReadBookConfig;

public class ReadSettingDialog extends Dialog {

    private ImageView fontDown;
    private ImageView fontUp;
    private ImageView spaceSmall;
    private ImageView spacemiddle;
    private ImageView spaceLarge;

    private ImageView bg_set_default;
    private ImageView bg_set_green;
    private ImageView bg_set_purple;
    private ImageView bg_set_blue;
    private ImageView bg_set_gray;

    private ReadBookActivity context;

    public ReadSettingDialog(ReadBookActivity context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setCanceledOnTouchOutside(false);
        this.context = context;
        setContentView(R.layout.book_setting);

        fontDown = (ImageView) findViewById(R.id.font_down);
        fontUp = (ImageView) findViewById(R.id.font_up);

        spaceSmall = (ImageView) findViewById(R.id.block_small);
        spacemiddle = (ImageView) findViewById(R.id.block_mid);
        spaceLarge = (ImageView) findViewById(R.id.block_large);

        bg_set_default = (ImageView) findViewById(R.id.bg_set_brown);
        bg_set_green = (ImageView) findViewById(R.id.bg_set_green);
        bg_set_purple = (ImageView) findViewById(R.id.bg_set_purple);
        bg_set_blue = (ImageView) findViewById(R.id.bg_set_blue);
        bg_set_gray = (ImageView) findViewById(R.id.bg_set_gray);

        initView();

        handleFont();

        handleSpace();
        
        handleBg();
    }

    private void initView() {

        switch (ReadBookConfig.getInstance().getLineSpace()) {
        case Constant.config_space_size_small:
            spaceSmall.setImageResource(R.drawable.block_small_hover);
            break;
        case Constant.config_space_size_mid:
            spacemiddle.setImageResource(R.drawable.block_mid_hover);
            break;
        case Constant.config_space_size_large:
            spaceLarge.setImageResource(R.drawable.block_large_hover);
            break;
        }

        switch (ReadBookConfig.getInstance().getBg()) {
        case Constant.config_bg_type_default:
            bg_set_default.setImageResource(R.drawable.default_selected);
            break;
        case Constant.config_bg_type_green:
            bg_set_green.setImageResource(R.drawable.green_selected);
            break;
        case Constant.config_bg_type_purple:
            bg_set_purple.setImageResource(R.drawable.purple_selected);
            break;
        case Constant.config_bg_type_blue:
            bg_set_blue.setImageResource(R.drawable.blue_selected);
            break;
        case Constant.config_bg_type_gray:
            bg_set_gray.setImageResource(R.drawable.gray_selected);
            break;
        }

    }

    public void handleBg() {
        bg_set_default.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View paramView) {
                bg_set_default.setImageResource(R.drawable.default_selected);
                bg_set_green.setImageResource(R.drawable.green);
                bg_set_purple.setImageResource(R.drawable.purple);
                bg_set_blue.setImageResource(R.drawable.blue);
                bg_set_gray.setImageResource(R.drawable.gray);
                ReadBookConfig.getInstance().setBg(Constant.config_bg_type_default);
                context.pagefactory.setBgBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), Constant.config_bg_type_default), 
                        EndUser.getInstance().screenWidth, EndUser.getInstance().screenHeight, true));
                refreshPage();
            }
        });
        bg_set_green.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View paramView) {
                bg_set_default.setImageResource(R.drawable.bg_default);
                bg_set_green.setImageResource(R.drawable.green_selected);
                bg_set_purple.setImageResource(R.drawable.purple);
                bg_set_blue.setImageResource(R.drawable.blue);
                bg_set_gray.setImageResource(R.drawable.gray);
                ReadBookConfig.getInstance().setBg(Constant.config_bg_type_green);
                context.pagefactory.setBgBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), Constant.config_bg_type_green), 
                        EndUser.getInstance().screenWidth, EndUser.getInstance().screenHeight, true));
                refreshPage();
            }
        });
        bg_set_purple.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View paramView) {
                bg_set_default.setImageResource(R.drawable.bg_default);
                bg_set_green.setImageResource(R.drawable.green);
                bg_set_purple.setImageResource(R.drawable.purple_selected);
                bg_set_blue.setImageResource(R.drawable.blue);
                bg_set_gray.setImageResource(R.drawable.gray);
                ReadBookConfig.getInstance().setBg(Constant.config_bg_type_purple);
                context.pagefactory.setBgBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), Constant.config_bg_type_purple), 
                        EndUser.getInstance().screenWidth, EndUser.getInstance().screenHeight, true));
                refreshPage();
            }
        });
        bg_set_blue.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View paramView) {
                bg_set_default.setImageResource(R.drawable.bg_default);
                bg_set_green.setImageResource(R.drawable.green);
                bg_set_purple.setImageResource(R.drawable.purple);
                bg_set_blue.setImageResource(R.drawable.blue_selected);
                bg_set_gray.setImageResource(R.drawable.gray);
                ReadBookConfig.getInstance().setBg(Constant.config_bg_type_blue);
                context.pagefactory.setBgBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), Constant.config_bg_type_blue), 
                        EndUser.getInstance().screenWidth, EndUser.getInstance().screenHeight, true));
                refreshPage();
            }
        });
        bg_set_gray.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View paramView) {
                bg_set_default.setImageResource(R.drawable.bg_default);
                bg_set_green.setImageResource(R.drawable.green);
                bg_set_purple.setImageResource(R.drawable.purple);
                bg_set_blue.setImageResource(R.drawable.blue);
                bg_set_gray.setImageResource(R.drawable.gray_selected);
                ReadBookConfig.getInstance().setBg(Constant.config_bg_type_gray);
                context.pagefactory.setBgBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), Constant.config_bg_type_gray), 
                        EndUser.getInstance().screenWidth, EndUser.getInstance().screenHeight, true));
                refreshPage();
            }
        });
    }
    
    
    public void handleSpace() {
        spaceSmall.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View paramView) {
                // TODO Auto-generated method stub
                spaceSmall.setImageResource(R.drawable.block_small_hover);
                spacemiddle.setImageResource(R.drawable.block_mid);
                spaceLarge.setImageResource(R.drawable.block_large);
                ReadBookConfig.getInstance().setLineSpace(Constant.config_space_size_small);
                context.pagefactory.m_nLineSpaceing = Constant.config_space_size_small;
                
                refreshPage();
            }
        });

        spacemiddle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View paramView) {
                spaceSmall.setImageResource(R.drawable.block_small);
                spacemiddle.setImageResource(R.drawable.block_mid_hover);
                spaceLarge.setImageResource(R.drawable.block_large);
                ReadBookConfig.getInstance().setLineSpace(Constant.config_space_size_mid);
                context.pagefactory.m_nLineSpaceing = Constant.config_space_size_mid;
                
                refreshPage();
            }
        });

        spaceLarge.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View paramView) {
                spaceSmall.setImageResource(R.drawable.block_small);
                spacemiddle.setImageResource(R.drawable.block_mid);
                spaceLarge.setImageResource(R.drawable.block_large_hover);
                ReadBookConfig.getInstance().setLineSpace(Constant.config_space_size_large);
                context.pagefactory.m_nLineSpaceing = Constant.config_space_size_large;
                
                refreshPage();
            }
        });
    }

    private void handleFont() {

        fontDown.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View paramView) {

                if (ReadBookConfig.getInstance().getFontSize() == Constant.config_font_size_1) {
                    Toast.makeText(context, "已经是最小的字体啦", Toast.LENGTH_SHORT).show();
                } else {
                    ReadBookConfig.getInstance().setFontSize(ReadBookConfig.getInstance().getFontSize() - 4);

                    context.pagefactory.m_fontSize = (int) ((int) ReadBookConfig.getInstance().getFontSize() * EndUser.getInstance().density);
                    context.pagefactory.mPaint.setTextSize(context.pagefactory.m_fontSize);
                    
                    refreshPage();
                }

            }
        });

        fontUp.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View paramView) {
                if (ReadBookConfig.getInstance().getFontSize() == Constant.config_font_size_5) {
                    Toast.makeText(context, "已经是最大的字体啦", Toast.LENGTH_SHORT).show();
                } else {
                    ReadBookConfig.getInstance().setFontSize(ReadBookConfig.getInstance().getFontSize() + 4);
                    context.pagefactory.m_fontSize = (int) ((int) ReadBookConfig.getInstance().getFontSize() * EndUser.getInstance().density);
                    context.pagefactory.mPaint.setTextSize(context.pagefactory.m_fontSize);
                    refreshPage();
                }
            }
        });

    }

    @Override
    public void onDetachedFromWindow() {
        
        super.onDetachedFromWindow();
        try {
            refreshPage();
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void refreshPage() {
        context.mPageWidget.invalidate();
        context.pagefactory.m_mbBufEnd = context.pagefactory.m_mbBufBegin;
        context.pagefactory.mLineCount = (int) (context.pagefactory.mVisibleHeight / (context.pagefactory.m_fontSize + context.pagefactory.m_nLineSpaceing)); 
        
        context.pagefactory.onDraw(context.mCurPageCanvas);
        context.pagefactory.onDraw(context.mNextPageCanvas);
        context.mPageWidget.setBitmaps(context.mCurPageBitmap, context.mNextPageBitmap);
    }
}
